home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr46 / strx221.zip / STRCMP.CPP < prev    next >
C/C++ Source or Header  |  1993-03-17  |  2KB  |  60 lines

  1. //
  2. // strcmp.cpp : Non-Ansi string routines
  3. // Author     : Roy S. Woll
  4. //
  5. // Copyright (c) 1993 by Roy S. Woll
  6. // You may distribute this source freely as long as you leave all files
  7. // in their original form, including the copyright notice as is.
  8. //
  9. // Description:
  10. //   Case insensitive compare and case conversion routines.
  11. //   These are not part of the standard ansi C library.  
  12. //   Vax-Vms and Unix systems should place the object file in their library.
  13. //
  14. // Date: Dec 4, 1992
  15. // Name: Roy S. Woll
  16. //
  17. #include <ctype.h>
  18.  
  19. int stricmp(const char * s1, const char * s2){
  20.    if (s1==s2) return 1;
  21.    if (!s1) return -1;
  22.    
  23.    while (*s1) if (toupper(*s1++)!=toupper(*s2++)) break;
  24.    if (*s1){
  25.       if (toupper(*--s1) > toupper(*--s2)) return 1;
  26.       else return -1;
  27.    }
  28.    else if (*s2) return -1;  // s2 is longer than s1
  29.    else return 0;
  30. };
  31.  
  32. int strnicmp(const char * s1, const char * s2, unsigned n){
  33.    if (s1==s2) return 1;
  34.    if (!s1) return -1;
  35.  
  36.    while ((*s1) && n--) if (toupper(*s1++)!=toupper(*s2++)) break;
  37.    if (!n) return 0;    // match
  38.  
  39.    if (*s1)  // search terminated due to difference
  40.    {
  41.       if (toupper(*--s1) > toupper(*--s2)) return 1;
  42.       else return -1;
  43.    }
  44.    else if (*s2) return -1;
  45.    else return 0;
  46. };
  47.  
  48. char * strlwr(char * s){ 
  49.    char * start = s;
  50.    do { *s = tolower(*s); } while (*s++); 
  51.    return start;
  52. };
  53.  
  54. char * strupr(char * s){ 
  55.    char * start = s;
  56.    do { *s = toupper(*s); } while (*s++); 
  57.    return start;
  58. };
  59.  
  60.